import 'package:get/get.dart';

class ApiException implements Exception {
  final int code;
  final String message;
  final String? details;

  ApiException({
    required this.code,
    required this.message,
    this.details,
  });

  @override
  String toString() {
    return 'ApiException: $message (code: $code)';
  }

  // 常见错误码定义
  static const int NETWORK_ERROR = -1;
  static const int TIMEOUT_ERROR = -2;
  static const int PARSE_ERROR = -3;
  static const int UNKNOWN_ERROR = -4;
  static const int SERVER_ERROR = -5;
  static const int UNAUTHORIZED = 401;
  static const int FORBIDDEN = 403;
  static const int NOT_FOUND = 404;
  static const int SERVER_INTERNAL_ERROR = 500;

  // 根据错误码获取友好的错误信息
  static String getErrorMessage(int code) {
    switch (code) {
      case NETWORK_ERROR:
        return 'network_connection_failed'.tr;
      case TIMEOUT_ERROR:
        return 'request_timeout'.tr;
      case PARSE_ERROR:
        return 'data_parsing_failed'.tr;
      case UNAUTHORIZED:
        return 'login_expired'.tr;
      case FORBIDDEN:
        return 'no_permission'.tr;
      case NOT_FOUND:
        return 'resource_not_found'.tr;
      case SERVER_INTERNAL_ERROR:
        return 'server_internal_error'.tr;
      default:
        return 'unknown_error'.tr;
    }
  }
}